/**
  学生信息管理详情页-基础信息组件
*/
<template>
    <div class="base-info-new-container">
        <div class="change-tip" v-if="hasChecked">
            (标
            <span class="color-block"></span>
            色字段为正在变更中...)
        </div>
        <div style="text-align: right;" v-if="globalEdit || superLimitNotStu">
            <el-button type="primary" style="margin-bottom: 10px;" @click="saveData()">{{ $t('student.save') }}</el-button>
        </div>
        <el-form :model="formInfo" ref="formInfo" class="base-info-form">
            <div class="base-info-new-content" v-for="(item, index) in baseInfo" :key="index">
                <table v-if="finished">
                    <tbody>
                        <tr>
                            <td colspan="6" style="padding: 7px;">{{ item.title }}</td>
                        </tr>
                        <tr v-for="(subItem, subIndex) in item.tableData" :key="subIndex">
                            <td :class="subItem.b.inReview ? ['field-checked', 'base-info-td-title'] : 'base-info-td-title'" v-if="subItem.a">
                                <font class="is-require" v-if="(subItem.b.isRequire && globalEdit && subItem.b.isEdit) || superLimitNotStu">*</font>
                                <span>{{ subItem.a }}</span>
                            </td>
                            <td :class="subItem.b.inReview ? ['field-checked', 'base-info-td-content'] : 'base-info-td-content'" v-if="subItem.b">
                                <span v-if="(globalEdit && subItem.b.isEdit) || superLimitNotStu">
                                   <el-form-item :prop="'ID-' + subItem.b.fieldId" :rules="[{validator: validator, trigger: subItem.b.type === 'select' ? 'change' : 'blur'}]">
                                       <edit-dom-info :fieldInfo="formInfo['ID-' + subItem.b.fieldId]" :options="options[subItem.b.value]" :citys="citys" :endStation="endStation"></edit-dom-info>
                                   </el-form-item>
                                </span>
                                <span v-else>{{ getValue(subItem.b) }}</span>
                            </td>
                            <td :class="subItem.d.inReview ? ['field-checked', 'base-info-td-title'] : 'base-info-td-title'" v-if="subItem.c">
                                <font class="is-require" v-if="(subItem.d.isRequire && globalEdit && subItem.d.isEdit) || superLimitNotStu">*</font>
                                <span>{{ subItem.c }}</span>
                            </td>
                            <td :class="subItem.d.inReview ? ['field-checked', 'base-info-td-content'] : 'base-info-td-content'" v-if="subItem.d">
                                <span v-if="(globalEdit && subItem.d.isEdit) || superLimitNotStu">
                                    <el-form-item :prop="'ID-' + subItem.d.fieldId" :rules="[{validator: validator, trigger: subItem.d.type === 'select' ? 'change' : 'blur'}]">
                                       <edit-dom-info :fieldInfo="formInfo['ID-' + subItem.d.fieldId]" :options="options[subItem.d.value]" :citys="citys" :endStation="endStation"></edit-dom-info>
                                    </el-form-item>
                                </span>
                                <span v-else>{{ getValue(subItem.d) }}</span>
                            </td>
                            <td colspan="2" rowspan="6" v-if="subIndex === 0 && item.tableName === 'student_basic_information_t'">
                                <!-- 照片开始 -->
                                <div class="photo-container">
                                    <!-- 个人照片开始 -->
                                    <div class="photo-container-parent">
                                        <div v-if="personalPhotoBase64" class="photo-img-container" style="">
                                            <img class="photo-img" :src="personalPhotoBase64"/>
                                        </div>
                                        <div class="photo-container-background" style="" v-else>
                                            <div class="pic-info">{{ $t('student.personalPhoto') }}</div>
                                        </div>
                                        <div v-if="globalEdit || superLimitNotStu" style="height: 32px;">
                                            <!-- <template v-if="canEditPersonalPhoto">
                                                <font class="is-require" v-if="formInfo['ID-3'].isRequire && globalEdit && formInfo['ID-3'].isEdit">*</font>
                                                <el-button type="text" @click="uploadPersonalPhoto()">{{ $t('student.upload') }}</el-button>
                                                <span>|</span>
                                                <el-button style="" type="text" @click="deletePersonalPhoto()">{{ $t('student.delete') }}</el-button>
                                            </template> -->
                                        </div>
                                    </div>
                                    <!-- 个人照片结束 -->
                                    <!-- 证件照片开始 -->
                                    <div class="photo-container-parent">
                                        <div v-if="certificatePhotoBase64" class="photo-img-container" style="">
                                            <img class="photo-img" :src="certificatePhotoBase64"/>
                                        </div>
                                        <div class="photo-container-background" v-else>
                                            <div class="pic-info">{{ $t('student.certificatePhoto') }}</div>
                                        </div>
                                        <div v-if="globalEdit || superLimitNotStu" style="height: 32px;">
                                            <template v-if="canEditCertificatePhoto">
                                                <font class="is-require" v-if="formInfo['ID-4'].isRequire && formInfo['ID-4'].isEdit">*</font>
                                                <el-button type="text" @click="uploadCertificatePhoto()">{{ $t('student.upload') }}</el-button>
                                                <span>|</span>
                                                <el-button type="text" @click="deleteCertificatePhoto()">{{ $t('student.delete') }}</el-button>
                                            </template>
                                        </div>
                                    </div>
                                    <!-- 证件照片结束 -->
                                </div>
                                <!-- 照片结束 -->
                            </td>
                            <td :class="subItem.f.inReview ? ['field-checked', 'base-info-td-title'] : 'base-info-td-title'" v-if="subItem.e">
                                <font class="is-require" v-if="(subItem.f.isRequire && globalEdit && subItem.f.isEdit) || superLimitNotStu">*</font>
                                <span>{{ subItem.e }}</span>
                            </td>
                            <td :class="subItem.f.inReview ? ['field-checked', 'base-info-td-content'] : 'base-info-td-content'" v-if="subItem.f">
                                <span v-if="(globalEdit && subItem.f.isEdit) || superLimitNotStu">
                                    <el-form-item :prop="'ID-' + subItem.f.fieldId" :rules="[{validator: validator, trigger: subItem.f.type === 'select' ? 'change' : 'blur'}]">
                                       <edit-dom-info :fieldInfo="formInfo['ID-' + subItem.f.fieldId]" :options="options[subItem.f.value]" :citys="citys" :endStation="endStation"></edit-dom-info>
                                    </el-form-item>
                                </span>
                                <span v-else>{{ getValue(subItem.f) }}</span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </el-form>
        <difference-dialog
            ref="diffDialog"
            :post-data="postData.stuDetailInfoList"
            :raw-data="originData"
            :options="options"
            :checked="checked"
            @save-diff="saveCheck"
            @update="getStuDetail"
            :citys="citys"
            :endStation="endStation"
        ></difference-dialog>
        <pic-upload-dialog
            title="照片上传"
            ref="photoUpload"
            v-if="canEditPersonalPhoto"
            @success="savePhoto"
        />
        <pic-upload-dialog
            title="证书照片上传"
            ref="certificateUpload"
            v-if="canEditCertificatePhoto"
            @success="saveCertificate"
        />
    </div>
</template>
<script>
import moment from 'moment'
import { submitField, getRules, getCitys, getCityNameByValue } from './businessMethods'
import { queryDic } from 'common/src/api/dictionary'
import {
    getStudentDetail,
    stuDetailSubmit,
    getStuInfoByParam,
    getStationInfoList
} from 'common/src/api/studentinfo'
import { getImageData } from 'common/src/api/teacherinfo'
import picUploadDialog from './picUploadDialog'
import differenceDialog from './differenceDialog'
import axios from 'axios'
import editDomInfo from './editDomInfo'
// import REST from './test'
export default {
    name: "baseInfoNew",
    props: ["tableInfo", "title", "superLimit", "isStudent", "statusInfo", "uid"],
    components: {
        editDomInfo,
        differenceDialog,
        picUploadDialog
    },
    data() {
        return {
            count: 0,
            globalEdit: true,
            superLimitNotStu: false,
            finished: true,
            baseInfo: [],
            options: {},
            citys: [],
            endStation: [],
            // 个人照片
            personalPhoto: '',
            personalPhotoBase64: '',
            canEditPersonalPhoto: false,
            // 证书照片
            certificatePhoto: '',
            certificatePhotoBase64: '',
            canEditCertificatePhoto: false,
            formInfo: {},
            // 原始数据
            originData: [],
            // 是否存在审核,控制显示差异弹框
            checked: false,
            // 最终的数据
            postData: {
                studentId: '',
                stuDetailInfoList: []
            },
            // 是否有审核字段,控制样式标色字段的提示
            hasChecked: false,
            // 支持可搜索字段集合-国家地区/籍贯/民族
            searchColumns: [14, 15, 9]
        };
    },
    mounted() {
        this.postData.studentId = this.uid
        this.getStuDetail()

         // 获取个人照片
        this.getPhoto()
    },
    methods: {
        // 字符校验
        validator(rule, value, callback) {
            // 必填性校验
            if (value.isRequire && !value.columnValue) {
                return callback(new Error(value.nameZH + '不能为空'))
            } else if (value.isRequire && Array.isArray(value.columnValue) && value.columnValue.length === 0) {
                return callback(new Error(value.nameZH + '不能为空'))
            } else if (value.columnValue && value.fieldId === 58 && this.formInfo['ID-57'].columnValue === '01' && value.legalityCheck && !new RegExp(value.legalityCheck).test(value.columnValue)) { // 父亲（或监护人）证件类型为身份证时,证件号码需遵循校验
                return callback(new Error(value.errorMessage || '输入的格式有误'))
            } else if (value.columnValue && value.fieldId === 61 && this.formInfo['ID-60'].columnValue === '01' && value.legalityCheck && !new RegExp(value.legalityCheck).test(value.columnValue)) { // 母亲（或监护人）证件类型为身份证时,证件号码需遵循校验
                return callback(new Error(value.errorMessage || '输入的格式有误'))
            } else if (value.columnValue && value.legalityCheck && !new RegExp(value.legalityCheck).test(value.columnValue) && value.fieldId !== 58 && value.fieldId !== 61) { // 正则校验,字段非父母身份证
                return callback(new Error(value.errorMessage || '输入的格式有误'))
            } else if (value.columnValue && value.dataType === 'float' && isNaN(value.columnValue)) { // 浮点数校验
                callback(new Error('请输入有效数字'))
            } else if (value.columnValue && value.dataType === 'long' && value.type !== 'date' && !/^\d+$/.test(value.columnValue)) { // 长型校验
                callback(new Error('请输入有效整数'))
            } else if (value.columnValue && value.dataType === 'int' && value.type !== 'date' && !/^\d+$/.test(value.columnValue)) { // 整型校验
                callback(new Error('请输入有效整数'))
            } else {
                return callback()
            }
        },

       async getPhoto() {
            let URL = '/api/transformer/picture/queryPicture'
            let formData = {
                'list':[this.uid]
                }
            const res = await axios.post(URL, formData, {
                    headers: { 'X-Token': localStorage.getItem('sessionid') || '' }
                })
                if (res && res.data && res.status === 200) {
                    this.personalPhotoBase64 = 'data:image/png;base64,' + res.data[this.uid]
                }
        },
        // 初始化数据
        initData(data) {
            // 初始化个人录取照片
            this.personalPhoto = ''
            this.personalPhotoBase64 = ''
            this.canEditPersonalPhoto = false
            // 初始化证书照片
            this.certificatePhoto = ''
            this.certificatePhotoBase64 = ''
            this.canEditCertificatePhoto = false
            this.baseInfo = _.cloneDeep(data)
            this.baseInfo.forEach(value => {
                value['originData'] = []
                value['tableData'] = []
            })
        },
        // 根据表名和学生ID查询详情信息
        async getStuDetail() {
            let _this = this
            this.count = 0
            this.initData(this.tableInfo)
            const stuResp = await getStudentDetail({
                studentId: this.uid,
                fieldClasses: this.tableInfo.map(value => {
                    return value.tableName
                })
            })
            // const stuResp = REST
            if (stuResp.code == 200 && stuResp.data && stuResp.data.length > 0) {
                this.originData = _.cloneDeep(stuResp.data)
                stuResp.data.map(value => {
                    if (value.stuAttriInfos && Array.isArray(value.stuAttriInfos) && value.stuAttriInfos.length > 0) {
                        value.stuAttriInfos.map(item => {
                            // 初始值columnValue拷贝
                            item['oldRecord'] = item.columnValue
                            // 有审核字段,控制样式标色字段的提示
                            if (item.inReview) {
                                this.hasChecked = true
                            }
                            // 处理字典数组
                            if (item.type === 'select' && item.value && !Object.keys(this.options).includes(item.value)) {
                                this.options[item.value] = []
                            }
                            // 处理多选的下拉框--健康状态为多选下拉框
                            if (item.type === 'select' && item.fieldId === 20) {
                                item.isMulti = true
                                if (item.columnValue !== null && item.columnValue !== '') {
                                    item.columnValue = item.columnValue.split(',')
                                }
                            }
                            // 处理支持可搜索的下拉框
                            if (item.type === 'select' && this.searchColumns.includes(item.fieldId)) {
                                item.isSearch = true
                            }
                            // 判定是否配置了录取照片且已上传录取照片,此处为objectKey
                            // if (item.fieldId === 3 && item.columnValue) {
                            //     this.personalPhoto = item.columnValue
                            // }
                            if ((item.fieldId === 3 && item.isEdit) || _this.superLimit) {
                                this.canEditPersonalPhoto = true
                            }
                            // 判定是否配置了证书照片且已上传证书照片,此处为objectKey
                            if (item.fieldId === 4 && item.columnValue) {
                                this.certificatePhoto = item.columnValue
                            }
                            if ((item.fieldId === 4 && item.isEdit) || _this.superLimit) {
                                this.canEditCertificatePhoto = true
                            }
                            // 备份原始数据
                            item['originValue'] = item.columnValue
                            // 写入字段
                            this.baseInfo.forEach(tableInfo => {
                                if (tableInfo.tableName === item.from) {
                                    if (this.isStudent && item.isEdit > 0) {
                                        _this.count++
                                    }
                                    _this.$set(_this.formInfo, 'ID-' + item.fieldId, item)
                                    // 非录取照片和证书照片录入数据
                                    if (item.fieldId !== 3 && item.fieldId !== 4) {
                                        tableInfo.originData.push(item)
                                    }
                                }
                            })
                        })
                    }
                })
                // 记录可编辑字段的个数，若可编辑字段的个数===接口返回的所有数据的长度，则此处不可编辑，保存按钮不显示
                if (this.superLimit) {
                    if (this.isStudent) {
                        if (this.count === 0) {
                            this.globalEdit = false
                        }
                    } else {
                        this.superLimitNotStu = true
                    }
                } else {
                    this.globalEdit = false
                }
                await this.getDictionaryData()
                // 表格布局处理数据
                this.finished = false
                this.baseInfo.forEach((tableInfo, index) => {
                    if (tableInfo.originData.length > 0) {
                        // 表格布局开始
                        // 学生信息表
                        // 为了完成布局，前12个字段为6行，一行2个字段，后面的字段为一行
                        if (tableInfo.tableName === 'student_basic_information_t') {
                            // 学生信息表前12个字段,排除照片字段
                            let prev12FieldsStuInfo = tableInfo.originData.slice(0, 12)
                            for (let i = 0; i < prev12FieldsStuInfo.length; i += 2) {
                                let currentTr = {
                                    a: prev12FieldsStuInfo[i].nameZH,
                                    b: prev12FieldsStuInfo[i]
                                }
                                if (prev12FieldsStuInfo[i + 1]) {
                                    currentTr['c'] = prev12FieldsStuInfo[i + 1].nameZH
                                    currentTr['d'] = prev12FieldsStuInfo[i + 1]
                                }
                                tableInfo.tableData.push(currentTr)
                            }
                            // 学生信息表12个字段以后,排除照片字段
                            let next12FieldsStuInfo = tableInfo.originData.slice(12)
                            for (let i = 0; i < next12FieldsStuInfo.length; i += 3) {
                                let currentTr = {
                                    a: next12FieldsStuInfo[i].nameZH,
                                    b: next12FieldsStuInfo[i]
                                }
                                if (next12FieldsStuInfo[i + 1]) {
                                    currentTr['c'] = next12FieldsStuInfo[i + 1].nameZH
                                    currentTr['d'] = next12FieldsStuInfo[i + 1]
                                }
                                if (next12FieldsStuInfo[i + 2]) {
                                    currentTr['e'] = next12FieldsStuInfo[i + 2].nameZH
                                    currentTr['f'] = next12FieldsStuInfo[i + 2]
                                }
                                tableInfo.tableData.push(currentTr)
                            }
                        } else {
                            // 个人联系信息及家庭信息
                            for (let i = 0; i < tableInfo.originData.length; i += 3) {
                                let currentTr = {
                                    a: tableInfo.originData[i].nameZH,
                                    b: tableInfo.originData[i]
                                }
                                if (tableInfo.originData[i + 1]) {
                                    currentTr['c'] = tableInfo.originData[i + 1].nameZH
                                    currentTr['d'] = tableInfo.originData[i + 1]
                                }
                                if (tableInfo.originData[i + 2]) {
                                    currentTr['e'] = tableInfo.originData[i + 2].nameZH
                                    currentTr['f'] = tableInfo.originData[i + 2]
                                }
                                tableInfo.tableData.push(currentTr)
                            }
                        }
                        // 表格布局结束
                    }
                })
                this.finished = true
                // 加载个人照片
                // if (this.personalPhoto) {
                //     // this.loadImage('personalPhotoBase64', this.personalPhoto)
                //     this.personalPhotoBase64 = 'data:image/png;base64,' + this.personalPhoto

                // }
                // 加载证书照片
                if (this.certificatePhoto) {
                    this.loadImage('certificatePhotoBase64', this.certificatePhoto)
                }
            }
        },
        // 获取火车终点站
        async getStation() {
            this.endStation = []
            await getStationInfoList({}).then(res => {
                if (res.code == 200 && res.data.length > 0) {
                    res.data.forEach(station => {
                        this.endStation.push({
                            label: station.provice + '-' + station.station,
                            value: station.id
                        })
                    })
                    this.$set(this.options, 'end_station', _.cloneDeep(this.endStation))
                }

            })

        },
        // 获取字典
        async getDictionaryData() {
            // 筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: Object.keys(this.options)
            }
            let _that = this
            const dicResp = await queryDic(data)
            if (dicResp.code == 200 && dicResp.data) {
                let keyword = '其他'
                for (const key in dicResp.data) {
                    let items = []
                    let obj = dicResp.data[key]
                    for (const objKey in obj) {
                        items.push({
                            value: objKey,
                            label: obj[objKey]
                        })
                    }
                    items = _.sortBy(items, ['value'])
                    items = items
                        .filter(o => o.label.indexOf(keyword) == -1)
                        .concat(items.filter(o => o.label.indexOf(keyword) != -1))
                    this.$set(this.options, key, items)
                }
                // 处理详细区域
                this.citys = getCitys(this.options)
                // 获取火车终点站
                await this.getStation()
            }
        },
        // 检查是否有改动
        checkIsDiff() {
            let _this = this
            let hasDiff = false
            for (let key in _this.formInfo) {
                if (_this.formInfo[key].columnValue != _this.formInfo[key].originValue) {
                    // 对于多选情况另外处理
                    if (_this.formInfo[key].isMulti && Array.isArray(_this.formInfo[key].columnValue) && _this.formInfo[key].columnValue.length === 0 && !_this.formInfo[key].originValue) {
                        continue
                    }
                    hasDiff = true
                }
            }
            return hasDiff
        },
        // 根据类型以及字段名填入对应的值
        // fieldObj字段对象，dicKey字典key，domType为dom类型，keyName为表中所对应的字段名
        getValue(fieldObj) {
            // 下拉框
            if (fieldObj.type === 'select') {
                if (fieldObj.value === 'G_XZQY') { // 籍贯、详细区域
                    return this.getCityName(fieldObj.columnValue)
                } else if (fieldObj.value === 'G_GJQU') { // 国家地区
                    return this.getTextByGJDQ(fieldObj.columnValue, this.options.G_GJQU)
                } else if (fieldObj.value === 'end_station') { // 火车终点站
                    return this.getTextByGJDQ(fieldObj.columnValue, this.endStation)
                } else {
                    return this.getTextByValue(fieldObj.columnValue, this.options[fieldObj.value], fieldObj.isMulti)
                }
            }
            // 日期
            if (fieldObj.type === 'date') {
                return this.getFormatDate(fieldObj.columnValue)
            }
            return fieldObj.columnValue
        },
        // 获取城市
        getCityName(val) {
            return getCityNameByValue(val, this.citys)
        },
        // 当地点columnValue为三位数
        getTextByGJDQ(val, array) {
            if (val == null) {
                return null;
            }
            if (!array) {
                return null;
            }
            let option = array.find(_ => _.value == val);
            return option ? option.label : null;
        },
        // 下拉框数据处理
        getTextByValue(val, array, isMulti) {
            if (val == null) {
                return null;
            }
            if (!array) {
                return null;
            }
            if (isMulti) {
                let values = val.toString().split(",");
                let label = [];
                values.forEach(v => {
                    let option = array.find(_ => _.value == v);
                    if (option) {
                        label.push(option.label);
                    }
                });
                return label.join(",");
            } else {
                let option = array.find(_ => _.value == val);
                return option ? option.label : null;
            }
        },
        // 处理日期格式
        getFormatDate(val) {
            return val ? moment(val).format('YYYY-MM-DD') : null
        },
        // 加载照片
        loadImage(field, objectKey) {
            getImageData(objectKey).then(response => {
                if (response.code === 200 && response.data) {
                    this.$set(this, field, response.data.base)
                }
            })
        },
        // 上传个人照片
        uploadPersonalPhoto() {
            this.$refs.photoUpload.open()
        },
        // 保存个人照片
        savePhoto(url) {
            this.personalPhoto = url
            this.loadImage('personalPhotoBase64', this.personalPhoto)
        },
        // 删除个人照片
        deletePersonalPhoto() {
            this.personalPhoto = ''
            this.personalPhotoBase64 = ''
        },
        // 上传证书照片
        uploadCertificatePhoto() {
            this.$refs.certificateUpload.open()
        },
        // 保存证书照片
        saveCertificate(url) {
            this.certificatePhoto = url
            this.loadImage('certificatePhotoBase64', this.certificatePhoto)
        },
        // 删除证书照片
        deleteCertificatePhoto() {
            this.certificatePhoto = ''
            this.certificatePhotoBase64 = ''
        },
        // 保存审核字段
        saveCheck(fileList, modifyDesc) {
            submitField(
                this.uid,
                fileList,
                modifyDesc,
                _.cloneDeep(this.postData),
                this.originData,
                this.saveAfter
            )
        },
        saveAfter() {
            this.$refs.diffDialog.close()
            this.$message({
                type: 'success',
                message: this.$t('message.saveSuccess')
            })
            setTimeout(() => {
                this.getStuDetail()
            }, 500)
        },
        // 检查全日制父母信息
        checkAllDayParentInfo() {
            // 是否开启父亲信息验证
            let fatherInfoCheck = false
            let motherInfoCheck = false
            // 父亲信息验证状态
            let fatherCheckStatus = false
            let motherCheckStatus = false
            // fieldId===56为父亲（或监护人）姓名,fieldId===58为父亲（或监护人）证件号码,fieldId===57为父亲（或监护人）证件类型
            if (
                this.formInfo['ID-56'] &&
                this.formInfo['ID-58'] &&
                this.formInfo['ID-57']
            ) {
                fatherInfoCheck = true
            }
            // fieldId===59为母亲（或监护人）姓名,fieldId===61为母亲（或监护人）证件号码,fieldId===60为母亲（或监护人）证件类型
            if (
                this.formInfo['ID-59'] &&
                this.formInfo['ID-61'] &&
                this.formInfo['ID-60']
            ) {
                motherInfoCheck = true
            }
            // 未开启验证，直接返回true
            if (!fatherInfoCheck && !motherInfoCheck) {
                return true
            }
            // 开始验证
            let notEmpty = column => {
                return column.columnValue != null && column.columnValue != ''
            }
            // fieldId===56为父亲（或监护人）姓名,fieldId===58为父亲（或监护人）证件号码,fieldId===57为父亲（或监护人）证件类型
            if (
                (fatherInfoCheck &&
                notEmpty(this.formInfo['ID-56']) &&
                notEmpty(this.formInfo['ID-58']) &&
                notEmpty(this.formInfo['ID-57'])) ||
                (fatherInfoCheck &&
                !notEmpty(this.formInfo['ID-56']) &&
                !notEmpty(this.formInfo['ID-58']) &&
                !notEmpty(this.formInfo['ID-57']))
            ) {
                fatherCheckStatus = true
            }
            // fieldId===59为母亲（或监护人）姓名,fieldId===61为母亲（或监护人）证件号码,fieldId===60为母亲（或监护人）证件类型
            if (
                (motherInfoCheck &&
                notEmpty(this.formInfo['ID-59']) &&
                notEmpty(this.formInfo['ID-61']) &&
                notEmpty(this.formInfo['ID-60'])) ||
                (motherInfoCheck &&
                !notEmpty(this.formInfo['ID-59']) &&
                !notEmpty(this.formInfo['ID-61']) &&
                !notEmpty(this.formInfo['ID-60']))
            ) {
                motherCheckStatus = true
            }
            if (!fatherCheckStatus) {
                this.$message({
                    type: 'warning',
                    message: '父亲或母亲的姓名、证件类型、证件号码需同时填写且至少填写一方的信息'
                })
                return false
            }
            if (!motherCheckStatus) {
                this.$message({
                    type: 'warning',
                    message: '父亲或母亲的姓名、证件类型、证件号码需同时填写且至少填写一方的信息'
                })
                return false
            }
            return true
        },
        // 保存数据
        saveData() {
            this.$refs['formInfo'].validate(valid => {
                if (valid) {
                    // 校验个人照片
                    // if (this.formInfo['ID-3'] && this.formInfo['ID-3'].isRequire && this.formInfo['ID-3'].isEdit && !this.personalPhoto) {
                    //     this.$message.error('个人照片未提交，请补全')
                    //     return
                    // }
                    // 校验证件照片
                    if (this.formInfo['ID-4'] && this.formInfo['ID-4'].isRequire && this.formInfo['ID-4'].isEdit && !this.certificatePhoto) {
                        this.$message.error('证件照片未提交，请补全')
                        return
                    }
                    this.postData.stuDetailInfoList = []
                    // 处理传参开始
                    let hasContactInfo = false
                    let formData = _.cloneDeep(this.formInfo)
                    let originData = _.cloneDeep(this.originData)
                    originData.forEach((value, index, array) => {
                        let hasDiff = false
                        let currentObject = {
                            id: value.id,
                            operType: value.id ? 2 : 1, // 基础信息，只有两种操作类型：新增、更新,
                            stuAttriInfos: []
                        }
                        if (value.stuAttriInfos.length > 0) {
                            value.stuAttriInfos.forEach(stuItem => {
                                let formDataKey = 'ID-' + stuItem.fieldId
                                if (formData[formDataKey]) {
                                    if (stuItem.columnValue === null && formData[formDataKey].columnValue === '') {
                                       formData[formDataKey].columnValue = null
                                    }
                                    // 处理多选传参
                                    if (formData[formDataKey].isMulti && formData[formDataKey].columnValue) {
                                        formData[formDataKey].columnValue = formData[formDataKey].columnValue.length > 0 ? formData[formDataKey].columnValue.join(',') : null
                                    }
                                    // 赋值录取照片fieldId为3/证件照片fieldId为4
                                    // if (stuItem.fieldId === 3 && this.personalPhoto) {
                                    //     formData['ID-3'].columnValue = this.personalPhoto
                                    // }
                                    if (stuItem.fieldId === 4 && this.certificatePhoto) {
                                        formData['ID-4'].columnValue = this.certificatePhoto
                                    }
                                    // 表名相同同时字段相同,值不同,存入数组
                                    if (stuItem.from === formData[formDataKey].from && stuItem.fieldId === formData[formDataKey].fieldId && stuItem.columnValue !== formData[formDataKey].columnValue) {
                                        hasDiff = true
                                        // 修改了个人联系信息及家庭信息
                                        if (formData[formDataKey].from === 'student_contact_information_t') {
                                            hasContactInfo = true
                                        }
                                        if (stuItem.isMulti) {
                                            formData[formDataKey].oldColumnValue = stuItem.columnValue.join(',')
                                        } else {
                                            formData[formDataKey].oldColumnValue = stuItem.columnValue
                                        }
                                        // newRealValue:下拉框的label
                                        // oldRealValue:下拉框的label
                                        if (formData[formDataKey].isEdit && formData[formDataKey].type == 'select' && formData[formDataKey].value) {
                                            let newOpt = this.options[formData[formDataKey].value].find(
                                                option => option.value == formData[formDataKey].columnValue
                                            )
                                            let oldOpt = this.options[formData[formDataKey].value].find(
                                                option => option.value == formData[formDataKey].oldColumnValue
                                            )
                                            if (newOpt) {
                                                formData[formDataKey].newRealValue = newOpt.label
                                            }
                                            if (oldOpt) {
                                                formData[formDataKey].oldRealValue = oldOpt.label
                                            }
                                        }
                                        // 记录oldColumnValue
                                        formData[formDataKey].oldColumnValue = formData[formDataKey].oldRecord
                                        // 教务员可编辑模式下 需要把isEdit参数 置为2 传给后端
                                        if(this.superLimit && !this.isStudent) {
                                            formData[formDataKey].isEdit = 2
                                        }
                                        currentObject.stuAttriInfos.push(formData[formDataKey])
                                        // 存在审核
                                        if (formData[formDataKey].isSchoolCheck || formData[formDataKey].isCollegeCheck) {
                                            this.checked = true
                                        }
                                    }
                                }
                            })
                        }
                        // 字段值有差异
                        if (hasDiff) {
                            this.postData.stuDetailInfoList.push(currentObject)
                        }
                    })

                    // 字段值无差异
                    if (this.postData.stuDetailInfoList.length === 0) {
                        this.$message('内容未修改，无需保存')
                        return
                    }
                    // 修改了个人联系信息及家庭信息则要校验全日制父母信息,全日制编码formLearning为1
                    if (hasContactInfo && this.statusInfo && this.statusInfo.formLearning && this.statusInfo.formLearning === '1') {
                        let result = this.checkAllDayParentInfo()
                        if (!result) return
                    }
                    // 处理传参结束
                    this.$confirm('您确定要提交吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        // 存在审核
                        if (this.checked) {
                            setTimeout(() => {
                                this.$refs.diffDialog.open()
                            }, 300)
                        } else {
                            stuDetailSubmit(this.postData).then(res => {
                                if (res.code == 200) {
                                    this.$refs.diffDialog.open()
                                }
                            })
                        }
                    })
                } else {
                    setTimeout(() => {
                        let isError = document.getElementsByClassName("is-error")
                        if (isError[0] && isError[0] && isError[0].querySelector('input')) {
                            isError[0].querySelector('input').focus()
                        }
                    }, 1)
                }
            })
        }
    },
    watch: {
        tableInfo: {
            handler(newVal) {
                if (newVal) {
                    this.initData(newVal)
                }
            },
            deep: true,
            immediate: true
        }
    }
};
</script>
<style lang="scss" scoped>
.base-info-new-container ::v-deep {
    // margin-top: 10px;
    .change-tip {
        line-height: 32px;
        font-size: 16px;
        float: left;
        margin-left: 15px;
        .color-block {
            background: #ff7800;
            display: inline-block;
            width: 25px;
            height: 12px;
            margin: 0 5px 0 0;
        }
    }
    .field-checked {
        font-weight: bold;
        color: #ff7800;
    }
    .base-info-form {
        padding-bottom: 7px;
        .el-form-item {
            // margin-bottom: 13px!important;
            margin-bottom: 0px!important;
        }
    }
    .base-info-new-content {
        position: relative;
        table {
            width: 100%;
            border-collapse: collapse;
            .is-require {
                color: #f56c6c;
            }
            td {
                padding: 7px 4px;
                border: 1px solid #e4e7ed;
            }
            .base-info-td-title {
                width: 14.11%;
                text-align: right;
                background: #F5F7F9;
            }
            .base-info-td-content {
                width: 19.22%;
            }
        }
    }
    .photo-container {
        text-align: center;
        .photo-container-parent {
            display: inline-block;
            vertical-align: middle;
            margin: 0 10px;
            .photo-img-container {
                width: 120px;
                height: 150px;
                border: 1px solid #e4e7ed;
            }
            .photo-img {
                width: 112px;
                height: 142px;
                margin-top: 3px;
            }
            .photo-container-background {
                display: inline-block;
                width: 120px;
                height: 150px;
                background: #fff;
                color: #6c7685;
                line-height: 142px;
                font-size: 15px;
                font-weight: bold;
                border: 1px solid #e4e7ed;
                padding: 3px;
                .pic-info {
                    background: #e4e7ed;
                }
            }
        }
    }
}
</style>
